#include <iostream>
using namespace std;

class Base {
public:
	Base (void):m_i (0) {}
	Base (int i):m_i (i) {}
	Base (const Base& that):m_i (that.m_i) {
		cout << "Base::Base(const Base& that)" << endl;
	}
	Base& operator= (const Base& that) {
		cout << "Base::operator=" << endl;
		if (&that != this) {
			m_i = that.m_i;
		}
		return *this;
	}
	int m_i;
};
class Derived:public Base {
public:
	Derived(void):m_i (0) {}
	Derived(int i1, int i2):Base(i1), m_i(i2) {}
	//没有指明基类子对象的初始化方式, 会调用积累中的无参拷贝构造, 就会出问题
	//Derived(const Derived& that):m_i (that.m_i) {}
	//Base (that): 指明基类子对象以拷贝的方式进行初始化
	//Base (that): 实参是子类类型的引用, 形参是基类类型的引用, 向上造型
	Derived(const Derived& that):Base(that), m_i(that.m_i) {}
	Derived& operator= (const Derived& that) {
		//只拷贝子类自己的对象, 也会出错
		if (&that != this) {
			m_i = that.m_i;
			//这样写可以, 但是不具有通用性
			//Base::m_i = that.Base::m_i;
			Base::operator=(that);
		}
		return *this;
	}

	int m_i;
};

int main (void)
{
	Derived d1(100, 200);
	cout << d1.Base::m_i << ',' << d1.m_i << endl;
	Derived d2(d1);
	cout << d2.Base::m_i << ',' << d2.m_i << endl;

	return 0;
}
